package com.tencent.shadow.sample.host.repository.manager;

import android.util.ArrayMap;

import androidx.lifecycle.MutableLiveData;

import com.google.gson.reflect.TypeToken;
import com.meichis.mcsappframework.entity.AppVersion;
import com.meichis.mcsappframework.utils.MessageTools;
import com.tencent.shadow.sample.host.common.DataCenter;
import com.tencent.shadow.sample.host.http.CallSubscriber;
import com.tencent.shadow.sample.host.http.IWebServiceCallback;
import com.tencent.shadow.sample.host.http.RetrofitUtil.RxSchedulersHelper;
import com.tencent.shadow.sample.host.repository.ApiUtil;

/**
 * @author xmy
 * @title
 * @remake
 * @create 2021-04-14 22:51
 */
public class AppUpdataManager extends BaseManager {

    private static AppUpdataManager INSTANCE;

    public static AppUpdataManager getInstance() {
        if (INSTANCE == null) {
            synchronized (AppUpdataManager.class) {
                if (INSTANCE == null) {
                    INSTANCE = new AppUpdataManager();
                }
            }
        }
        return INSTANCE;
    }

    public void clearInstance() {
        INSTANCE = null;
    }

    public void GetLastVersionInfo(final MutableLiveData<Boolean> showDialog, MutableLiveData<AppVersion> data) {
        showDialog.setValue(true);
        ArrayMap<String, Object> hs = new ArrayMap<>();
        //MDS经销商ID
        hs.put("AppCode", DataCenter.AppCode);
        getRootApi().GetLastVersionInfo(getToken(), ApiUtil.getRequestPack(hs))
                .compose(RxSchedulersHelper.rxSchedulerHelper())
                .subscribe(new CallSubscriber(new TypeToken<AppVersion>() {
                }.getType(), new IWebServiceCallback<AppVersion>() {
                    @Override
                    public void onSucceed(int sequence, AppVersion resultPack, String ReturnInfo, int Return) {
                        data.setValue(resultPack);
                        showDialog.setValue(false);
                    }

                    @Override
                    public void onFailure(int sequence, int errorCode, String error) {
                        showDialog.setValue(false);
                        MessageTools.showLongToast(error);
                    }
                }));
    }

    public void GetLastVersionInfo(final MutableLiveData<Boolean> showDialog, MutableLiveData<AppVersion> data, MutableLiveData<String> errorData) {
        showDialog.setValue(true);
        ArrayMap<String, Object> hs = new ArrayMap<>();
        //MDS经销商ID
        hs.put("AppCode", DataCenter.AppCode);
        getRootApi().GetLastVersionInfo(getToken(), ApiUtil.getRequestPack(hs))
                .compose(RxSchedulersHelper.rxSchedulerHelper())
                .subscribe(new CallSubscriber(new TypeToken<AppVersion>() {
                }.getType(), new IWebServiceCallback<AppVersion>() {
                    @Override
                    public void onSucceed(int sequence, AppVersion resultPack, String ReturnInfo, int Return) {
                        data.setValue(resultPack);
                        showDialog.setValue(false);
                    }

                    @Override
                    public void onFailure(int sequence, int errorCode, String error) {
                        showDialog.setValue(false);
                        MessageTools.showLongToast(error);
                        errorData.setValue(error);
                    }
                }));
    }

}
